
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;

/*
 * ClusterViewer.java
 *
 * Created on November 6, 2007, 8:24 AM
 */


/**
 *
 * @author  asinghal
 */
public class ClusterViewer  extends javax.swing.JFrame {
    
    /** Creates new form ClusterViewer */
    public ClusterViewer() {	
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        clusterTree = new javax.swing.JTree();
        distancesTable = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        posList = new javax.swing.JList(p.posNameArray);
        jScrollPane4 = new javax.swing.JScrollPane();
        filePreviewTextArea = new javax.swing.JEditorPane();
	//        selectAllPOSButton = new javax.swing.JButton();
	inputK = new javax.swing.JTextField();
        selectPOSOfInterestButton = new javax.swing.JButton();
        clusterButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        clusterTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clusterTreeMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(clusterTree);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        distancesTable.setViewportView(jTable1);

	/*        posList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
	    });*/

        posList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                posListValueChanged(evt);
            }
        });

        jScrollPane3.setViewportView(posList);

	/*        filePreviewTextArea.setColumns(20);
		  filePreviewTextArea.setRows(5);*/
        jScrollPane4.setViewportView(filePreviewTextArea);

	/*        selectAllPOSButton.setLabel("Select All");
        selectAllPOSButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selectAllPOSButtonMouseClicked(evt);
            }
	    });*/

        selectPOSOfInterestButton.setLabel("''Interesting'");
        selectPOSOfInterestButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selectPOSOfInterestButtonMouseClicked(evt);
            }
        });

        clusterButton.setText("Cluster");
        clusterButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clusterButtonMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(clusterButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(selectPOSOfInterestButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                            .add(inputK, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(distancesTable, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(inputK)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(selectPOSOfInterestButton))
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 193, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(distancesTable, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(clusterButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selectPOSOfInterestButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selectPOSOfInterestButtonMouseClicked
// TODO add your handling code here:
	 int[] selectThese = new int[] {9, 10, 11, 12, 13, 26, 41, 42, 43, 44, 45, 46, 48, 49, 50, 51, 56, 57, 58, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84};

	posList.setSelectedIndices(selectThese);

    }//GEN-LAST:event_selectPOSOfInterestButtonMouseClicked

    /*    private void selectAllPOSButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selectAllPOSButtonMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_selectAllPOSButtonMouseClicked*/

    private void clusterButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clusterButtonMouseClicked
// TODO add your handling code here:		

	Object[] ar=posList.getSelectedValues();
	String[] poss = new String[ar.length];
	for (int i=0;i<ar.length;i++)
	    poss[i]=(String)ar[i];

	Set<Set<AkshatDocument>> s = p.makeClusters(poss,Integer.parseInt(inputK.getText()),false);
	Object[] objectArray = new Object[s.size()+1];
	objectArray[0]="Clusters";
	Iterator it1= s.iterator();
	int i=1;
	while(it1.hasNext()){
	    Set<AkshatDocument> c= (Set<AkshatDocument>)it1.next();
		String[] innerObjectArray = new String[c.size()+1];
		innerObjectArray[0]="cluster" + i;
		Iterator it2= c.iterator();
		int j=1;
		while(it2.hasNext()){
		    AkshatDocument currentDoc = (AkshatDocument)it2.next();
			innerObjectArray[j]=  currentDoc.name;
			j++;
		}
		objectArray[i]=innerObjectArray;
		i++;		
	}	 
	clusterTree.setModel(new DefaultTreeModel(processHierarchy(objectArray)));

	System.out.print("{");
	int[] sel = posList.getSelectedIndices();
	for(i=0;i<sel.length;i++)
	    System.out.print(sel[i] + ", ");
	System.out.print("}\n");

	/*
	//TODO:add code for creating table
	TableModel dataModel = new AbstractTableModel() {
		public int getColumnCount() { return p.n_documents +1 ; }
		public int getRowCount() { return p.n_documents +1 ;}
		public Object getValueAt(int row, int col) { 
		    if (row==0 && col==0)
			return "";
		    else if (row==0)
			return "";
		    else if (col==0)
			return "";
		    else
			return p.distances[row-1][col-1]; }
			};*/
	
    }//GEN-LAST:event_clusterButtonMouseClicked

    private void clusterTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clusterTreeMouseClicked
// TODO add your handling code here:
	try{
	String filename = p.inDir + "/" + clusterTree.getSelectionPath().getPathComponent(2);
	    filePreviewTextArea.setPage("file://" + filename);

	}
	catch(Exception e){
	    e.printStackTrace();
	}
    }//GEN-LAST:event_clusterTreeMouseClicked

    private void posListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_posListValueChanged
// TODO add your handling code here:
        
    }//GEN-LAST:event_posListValueChanged
    
    /**
     * @param args the command line arguments
     */
    static POSCluster p;
    public static void main(String args[]) throws Exception{
	p= new POSCluster();
	p.main(args);
		
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClusterViewer().setVisible(true);
            }
        });

	

    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clusterButton;
    private javax.swing.JTree clusterTree;
    private javax.swing.JScrollPane distancesTable;
    private javax.swing.JEditorPane filePreviewTextArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JList posList;
    private javax.swing.JButton selectAllPOSButton;
    private javax.swing.JButton selectPOSOfInterestButton;
    private javax.swing.JTextField inputK;

    // End of variables declaration//GEN-END:variables


    private static DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
	DefaultMutableTreeNode node =
	    new DefaultMutableTreeNode(hierarchy[0]);
	DefaultMutableTreeNode child;
	for(int i=1; i<hierarchy.length; i++) {
	    Object nodeSpecifier = hierarchy[i];
	    if (nodeSpecifier instanceof Object[])  // Ie node with children
		child = processHierarchy((Object[])nodeSpecifier);
	    else
		child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
	    node.add(child);
	}
	return(node);
    }    
}
